﻿<?php

class navygation_page 
{
	private $page_size = 7;
	private $link_padding = 10;
	private $page_link_separator = ' ';
	private $next_page_text = 'следующая';
	private $prev_page_text = 'предыдущая';
	private $result_text_pattern = 'Показано с %s по %s из %s';
	public $page_var = '';
	public $link_nachalo = '';
	
	private $db;
	private $q;
	public $total_rows;
	private $total_pages;
	private $cur_page;
	
	public function __construct($db, $link_nachalo='', $page_var='') {
		$this->db = $db;		
		$this->page_var = $page_var;
		$this->link_nachalo = $link_nachalo;
		$this->cur_page = isset($this->page_var) && (int)$this->page_var > 0 ? (int)$this->page_var : 1;
	}	
	public function set_query($q) {
		$this->q = $q;
	}
	public function set_page_size($page_size) {
		$this->page_size = abs((int)$page_size);
	}	
	public function set_link_padding($padding) {
		$this->link_padding = abs((int)$padding);
	}	
	public function get_page($q = '')
	{
		if($q) {
			self::set_query($q);
		}	
		$r = $this->db->query(self::query_paging($this->q));
		$this->total_rows = array_pop($this->db->query('SELECT FOUND_ROWS()')->fetchRow(MDB2_FETCHMODE_ASSOC));
	
		if($this->page_size !== 0) {
			$this->total_pages = ceil($this->total_rows/$this->page_size);
		}
		if ($this->cur_page > $this->total_pages) {
			$this->cur_page = $this->total_pages;
			if($this->total_pages > 0) {
				$r = $this->db->query(self::query_paging($this->q));
			}
		}		
		return $r;
	}	
	public function get_result_text()
	{
		$start = (($this->cur_page-1) * $this->page_size)+1;
		$end = (($start-1+$this->page_size) >= $this->total_rows)? $this->total_rows:($start-1+$this->page_size);	
		return sprintf($this->result_text_pattern, $start, $end, $this->total_rows);
	}
	
	public function get_page_links()
	{
		if(!isset($this->total_pages)) {
			return '';
		}
		$page_link_list = array();
	
		$start = $this->cur_page - $this->link_padding;
		if($start < 1) {
			$start = 1;
		}
		$end = $this->cur_page + $this->link_padding-1;
		if($end > $this->total_pages) {
			$end = $this->total_pages;
		}	
		if($start > 1) {
			$page_link_list[] = self::get_page_link($start-1, $start - 2 > 0 ? '...' : '');
		}
		for($i=$start; $i <= $end; $i++) {
			$page_link_list[] = self::get_page_link($i);
		}
		if($end + 1 < $this->total_pages) {
			$page_link_list[] = self::get_page_link($end +1, $end + 2 == $this->total_pages ? '' : '...');
		}
		if($end + 1 <= $this->total_pages) {
			$page_link_list[] = self::get_page_link($this->total_pages);	
		}
		return implode($this->page_link_separator, $page_link_list);
	}
	
	public function get_next_page_link() {
		return isset($this->total_pages) && $this->cur_page < $this->total_pages ? self::get_page_link($this->cur_page + 1, $this->next_page_text) : '';
	}	
	public function get_prev_page_link() {
		return isset($this->total_pages) && $this->cur_page > 1 ? self::get_page_link($this->cur_page - 1, $this->prev_page_text) : '';
	}	
	private function get_page_link($page, $text='')
	{
		if(!$text) {
			$text = $page;
		}
		if($page != $this->cur_page) {
			if($text == "следующая") {
				$class = ' class="modern-page-next"';
			}
			elseif($text == "предыдущая") {
				$class = ' class="modern-page-previous backward"';
			}
			else {
				$class = ' class="page'.(($page == 1) ? " modern-page-first" : '').'"';
			}
			return '<a href="/'.$this->link_nachalo.'/page/'.$page.ENDURL.'"'.$class.'>'.$text.'</a>';
		}
		return '<span class="modern-page-current'.(($page == 1) ? " modern-page-first" : '').'">'.$text.'</span>';
	}	
	private function query_paging()
	{
		$q = $this->q;	
		if($this->page_size != 0)
		{
			/* calculate the starting row */
			$start = ($this->cur_page-1) * $this->page_size;
			/* insert SQL_CALC_FOUND_ROWS and add the LIMIT */
			$q = preg_replace('/^SELECT\s+/i', 'SELECT SQL_CALC_FOUND_ROWS ', $this->q)." LIMIT {$start},{$this->page_size}";
		}	
		return $q;
	}
}
?> 